from itertools import pairwise
from typing import List


class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
        orderDict = {c: i for i, c in enumerate(order)}
        for i in range(len(words) - 1):
            word1 = words[i]
            word2 = words[i + 1]
            for j in range(min(len(word1), len(word2))):
                if orderDict[word1[j]] > orderDict[word2[j]]:
                    return False
                if orderDict[word1[j]] < orderDict[word2[j]]:
                    break
            else:
                if len(word1) > len(word2):
                    return False
        return True

    def isAlienSorted2(self, words: List[str], order: str) -> bool:
        index = {c: i for i, c in enumerate(order)}
        return all(s <= t for s, t in pairwise([index[c] for c in word] for word in words))